import 'dart:convert';

import 'package:fluent_ui/fluent_ui.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:path/path.dart';
import 'package:uuid/uuid.dart';

import '../../core/core.dart';
import 'ui_configs.dart';
import 'app_configs.dart';
import 'device_configs.dart';
import 'encrypt_configs.dart';
import 'task_configs.dart';

const k = 'ed97869ed31972dbf9a510dbf1032fc2';

abstract class Configs {
  static const _boxName = 'configs';
  static final app = AppConfigs();
  static final encrypt = EncryptConfigs();
  static final device = DeviceConfigs();
  static final task = TaskConfigs();
  static final ui = UIConfigs();

  static Future<void> initialize() async {
    Hive.init(join(Core.support.path, 'hive'));
    await Hive.openBox(
      _boxName,
      encryptionCipher: HiveAesCipher(utf8.encode(k)),
    );
    final now = DateTime.now().millisecondsSinceEpoch;
    final install = app.installDateTime.value;
    if (install == null) {
      app.installDateTime.value = now;
      app.appSerial.value = const Uuid().v4().replaceAll('-', '');
    }
    Configs.app.lastLaunchTime.value ??= now;
  }

  static void resetAll() {
    app.reset();
    encrypt.reset();
    device.reset();
    task.reset();
    ui.reset();
  }

  @protected
  static Box get box => Hive.box(_boxName);

  @protected
  void reset();
}
